package org.amphiprion.descentv2assistant.adapter;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.descentv2assistant.R;
import org.amphiprion.descentv2assistant.entity.GameSession;
import org.amphiprion.descentv2assistant.ui.views.controler.UndoBarController;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class SlidingMenuAdapter extends BaseAdapter {
	protected Context mApp;
	protected SherlockFragmentActivity mContext;
	private int mCurrentlyUpdating;
	private boolean mEdit;
	private OnEditModeListener mEditListner;
	protected LayoutInflater mInflater;
	protected List<GameSession> mItems;
	private UndoBarController mUndoBarController;
	private OnRemoveListener onRemoveListener;

	public SlidingMenuAdapter(SherlockFragmentActivity paramFragmentActivity, List<GameSession> paramArrayList) {
		this.mContext = paramFragmentActivity;
		this.mApp = paramFragmentActivity;
		this.mInflater = LayoutInflater.from(paramFragmentActivity);
		this.mItems = paramArrayList;

	}

	private void inflateView(View paramView, int index, ViewHolder paramViewHolder) {
		GameSession localGameSession = getItem(index);
		paramViewHolder.mGameSessionName.setText(localGameSession.getName());
		paramViewHolder.mNbHeroes.setText(localGameSession.getNumberOfHeroes() + " ");
		// String str = localGameSession.getPosterURL();
		// if ((!TextUtils.isEmpty(str)) && (!str.contains("null"))) {
		// App.setUrlDrawable(paramViewHolder.mImage, str,
		// ((FragmentActivity) mApp).getSupportFragmentManager());
		// }

		paramViewHolder.mRemove.setVisibility(mEdit ? View.VISIBLE : View.GONE);

	}

	@Override
	public int getCount() {
		if (this.mItems != null) {
			return this.mItems.size();
		} else {
			return 0;
		}
	}

	@Override
	public GameSession getItem(int paramInt) {
		return this.mItems.get(paramInt);
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	public List<GameSession> getGameSessions() {
		return this.mItems;
	}

	@Override
	public View getView(final int index, View paramView, ViewGroup paramViewGroup) {
		ViewHolder localViewHolder;
		if (paramView == null) {
			localViewHolder = new ViewHolder();
			paramView = this.mInflater.inflate(R.layout.item_game_session, null);
			localViewHolder.mGameSession = ((LinearLayout) paramView.findViewById(R.id.gameSession));
			localViewHolder.mGameSessionName = ((TextView) paramView.findViewById(R.id.gameSessionName));
			localViewHolder.mNbHeroes = ((TextView) paramView.findViewById(R.id.nbHeroes));

			// localViewHolder.mImage = ((ImageView) paramView
			// .findViewById(R.id.tvShow));
			localViewHolder.mRemove = ((ImageView) paramView.findViewById(R.id.remove));
			localViewHolder.mRemove.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (SlidingMenuAdapter.this.onRemoveListener != null) {
						SlidingMenuAdapter.this.onRemoveListener.hideShow(index);
						notifyDataSetChanged();
					}

				}
			});

			paramView.setTag(localViewHolder);
		} else {
			localViewHolder = (ViewHolder) paramView.getTag();
		}

		inflateView(paramView, index, localViewHolder);

		return paramView;
	}

	// protected void hideShow(int index) {
	// Series localSeries = remove(index);
	// // this.mApp.getDBAdapter().setTVShowVisibility(false,
	// // localSeries.getSeriesId());
	// this.mUndoBarController.showUndoBar(true, localSeries.getSeriesName(),
	// localSeries, index);
	// notifyDataSetChanged();
	// }

	public boolean isEditMode() {
		return this.mEdit;
	}

	public GameSession remove(int paramInt) {
		return this.mItems.remove(paramInt);
	}

	public void setEditListner(OnEditModeListener paramOnEditModeListener) {
		this.mEditListner = paramOnEditModeListener;
		this.mEditListner.onEditMode(this.mEdit);
	}

	public void setOnRemoveListener(OnRemoveListener onRemoveListener) {
		this.onRemoveListener = onRemoveListener;
	}

	public void setItems(ArrayList<GameSession> paramArrayList) {
		this.mItems = paramArrayList;
		notifyDataSetChanged();
	}

	public void setUpdating(int paramInt) {
		this.mCurrentlyUpdating = paramInt;
		notifyDataSetChanged();
	}

	public void toggleEdit(boolean paramBoolean) {
		this.mEdit = paramBoolean;
		notifyDataSetChanged();
		if (this.mEditListner != null) {
			this.mEditListner.onEditMode(this.mEdit);
		}
	}

	public boolean toggleEdit() {
		this.mEdit = !this.mEdit;
		toggleEdit(mEdit);
		return this.mEdit;

	}

	class ViewHolder {
		// ImageView mImage;
		public ImageView mRemove;
		TextView mGameSessionName;
		TextView mNbHeroes;
		LinearLayout mGameSession;

		ViewHolder() {
		}
	}

	public void setUndoController(UndoBarController paramUndoBarController) {
		this.mUndoBarController = paramUndoBarController;
	}

	public static abstract interface OnEditModeListener {
		public abstract void onEditMode(boolean paramBoolean);
	}

	public static abstract interface OnRemoveListener {
		void hideShow(int index);
	}
}